package java8Up.serializableDemo;

import java.io.*;

/**
 * @author Fang Ruichuan
 * @date 2022-10-11 17:19
 */

public class SerializableTest {
    private static void serialize(User user) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:\\IT学习\\Java成长\\java原理\\11.txt")));
        oos.writeObject(user);
        oos.close();
    }

    private static User deserialize() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\IT学习\\Java成长\\java原理\\11.txt")));
        return (User) ois.readObject();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        User user = new User();
//        user.setName("tyshawn");
//        user.setAge(18);
//        user.setSex("F");
//        System.out.println("序列化前的结果是：" + user);
//
//        serialize(user);

        User dUser = deserialize();
        System.out.println("反序列化后的结果是：" + dUser);
    }
}
